
# https://pyautogui.readthedocs.io/en/latest/
import pyautogui, time
pyautogui.FAILSAFE = True

def test1():
	print(pyautogui.size())
	print(pyautogui.position())

	print('moveTo(100, 100)')
	pyautogui.moveTo(100, 100)
	print('moveRel(500, 300, duration=3)')
	pyautogui.moveRel(500, 300, duration=3)
	print('click()')
	pyautogui.click()
	print('click(200,200,button="right")')
	pyautogui.click(200,200,button="right")
	print('moveRel(300, 0)')
	pyautogui.moveRel(300, 0)
	print('doubleClick()')
	pyautogui.doubleClick()
	pyautogui.click(clicks=3, interval=1)
	print('dragTo(100, 100, duration=1)')
	pyautogui.dragTo(100, 100, duration=1, button='left')
	print('dragRel(-100, -100, duration=1)')
	pyautogui.dragRel(100, 100, duration=1, button='left')

def test2():
	pyautogui.moveTo(300, 300)
	print('scroll(200)')
	pyautogui.scroll(-10)

	pyautogui.typewrite('Hello world', 0.25)
	pyautogui.copy('中文')
	pyautogui.hotkey('ctrl', 'c')

	pyautogui.keyDown('enter')
	pyautogui.keyUp('enter')

def test3():
	KEYS = ['t', 'n', 'r', ' ', '!', '"', '#', '$', '%', '&', "'", '(',
		')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', ':', ';', '<', '=', '>', '?', '@', '[', '', ']', '^', '_', '`',
		'a', 'b', 'c', 'd', 'e','f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
		'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
		'accept', 'add', 'alt', 'altleft', 'altright', 'apps', 'backspace',
		'browserback', 'browserfavorites', 'browserforward', 'browserhome',
		'browserrefresh', 'browsersearch', 'browserstop', 'capslock', 'clear',
		'convert', 'ctrl', 'ctrlleft', 'ctrlright', 'decimal', 'del', 'delete',
		'divide', 'down', 'end', 'enter', 'esc', 'escape', 'execute', 'f1', 'f10',
		'f11', 'f12', 'f13', 'f14', 'f15', 'f16', 'f17', 'f18', 'f19', 'f2', 'f20',
		'f21', 'f22', 'f23', 'f24', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9',
		'final', 'fn', 'hanguel', 'hangul', 'hanja', 'help', 'home', 'insert', 'junja',
		'kana', 'kanji', 'launchapp1', 'launchapp2', 'launchmail',
		'launchmediaselect', 'left', 'modechange', 'multiply', 'nexttrack',
		'nonconvert', 'num0', 'num1', 'num2', 'num3', 'num4', 'num5', 'num6',
		'num7', 'num8', 'num9', 'numlock', 'pagedown', 'pageup', 'pause', 'pgdn',
		'pgup', 'playpause', 'prevtrack', 'print', 'printscreen', 'prntscrn',
		'prtsc', 'prtscr', 'return', 'right', 'scrolllock', 'select', 'separator',
		'shift', 'shiftleft', 'shiftright', 'sleep', 'space', 'stop', 'subtract', 'tab',
		'up', 'volumedown', 'volumemute', 'volumeup', 'win', 'winleft', 'winright', 'yen',
		'command', 'option', 'optionleft', 'optionright']

	time.sleep(1)
	pyautogui.hotkey('command','escape')

def test4():
	# x, y = pyautogui.locateCenterOnScreen('1.png')
	# print(x, y)
	# pyautogui.moveTo(x//2, y//2)
	# pyautogui.screenshot()
	# pyautogui.screenshot('2.png')
	pyautogui.screenshot('2.png', region=(100, 100, 30, 30))
	

def test5():
	from selenium import webdriver
	from selenium.webdriver.common.touch_actions import TouchActions

	# # mobileEmulation = {'deviceName': 'Apple iPhone 5'}
	# # options = webdriver.ChromeOptions()
	# # options.add_experimental_option('mobileEmulation', mobileEmulation)
	# # driver = webdriver.Chrome(chrome_options=options)
	# driver = webdriver.Chrome()
	# driver.get('http://mi.com')
	# driver.maximize_window()
	# action = TouchActions(driver)

	# time.sleep(5)
	# driver.close()

	mobileEmulation = {'deviceName': 'iPhone 6'}
	options = webdriver.ChromeOptions()
	options.add_experimental_option('mobileEmulation', mobileEmulation)
	driver = webdriver.Chrome(chrome_options=options)
	driver.get('http://m.baidu.com')
	time.sleep(3)
	driver.close()

def test6():
	import time
	from selenium import webdriver
	from selenium.webdriver.common.action_chains import ActionChains
	from selenium.webdriver.common.by import By
	from selenium.webdriver.support.ui import WebDriverWait
	from selenium.webdriver.support import expected_conditions as EC

	WIDTH = 320
	HEIGHT = 640
	PIXEL_RATIO = 3.0
	mobileEmulation = {"deviceMetrics": {"width": WIDTH, "height": HEIGHT, "pixelRatio": PIXEL_RATIO}}
	options = webdriver.ChromeOptions()
	options.add_experimental_option('mobileEmulation', mobileEmulation)
	browser = webdriver.Chrome(options=options, executable_path='./chromedriver')
	browser.set_window_size(500, 1000)
	# browser.back()
	# browser.forward()
	# browser.refresh()

	browser.get('https://www.baidu.com/')
	# element = WebDriverWait(browser, 10).until(
	# 	EC.presence_of_element_located((By.TAG_NAME, "body"))
	# )

	# browser.execute_script('window.open("http://mi.com")')
	search_input = browser.find_element(By.ID, 'kw')
	search_input.send_keys("Cong Liu")
	# button = browser.find_element(By.XPATH, '//*[@id="search"]/div/div[2]/button')
	button = browser.find_element(By.ID, 'su')
	# button.click()

	x = int(button.location['x'])
	y = int(button.location['y'])
	width = int(button.size['width'])
	height = int(button.size['height'])
	print(x, y, width, height)
	actions = ActionChains(browser)
	actions.move_by_offset((x+width//2), (y+height//2)).click().perform()
	time.sleep(1)

	browser.get_screenshot_as_file("mac_pro.png")
	# body = browser.find_element(By.TAG_NAME, 'body')

	# for i in range(10):
	# 	time.sleep(1)
	# 	browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')

	time.sleep(10)
	browser.close()

test6()